home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / PATRON / PATRON.CPP < prev    next >
C/C++ Source or Header  |  1993-06-07  |  10KB  |  434 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Modifications for Chapter 5
  4.  *
  5.  * WinMain which is all we need for the basic application.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. //CHAPTER5MOD
  18. #define INITGUIDS
  19. //End CHAPTER5MOD
  20.  
  21. #include "patron.h"
  22.  
  23.  
  24.  
  25. /*
  26.  * WinMain
  27.  *
  28.  * Purpose:
  29.  *  Main entry point of application.   Should register the app class
  30.  *  if a previous instance has not done so and do any other one-time
  31.  *  initializations.
  32.  */
  33.  
  34. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  35.     , LPSTR pszCmdLine, int nCmdShow)
  36.     {
  37.     LPCPatronFrame  pFR;
  38.     FRAMEINIT       fi;
  39.     WPARAM          wRet;
  40.  
  41.     //Attempt to allocate and initialize the application
  42.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  43.  
  44.     fi.idsMin=IDS_FRAMEMIN;
  45.     fi.idsMax=IDS_FRAMEMAX;
  46.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  47.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  48.     fi.idStatMenuMin=ID_MENUFILE;
  49.     fi.idStatMenuMax=ID_MENUHELP;
  50.     fi.iPosWindowMenu=WINDOW_MENU;
  51.     fi.cMenus=CMENUS;
  52.  
  53.     //If we can initialize pFR, start chugging messages
  54.     if (pFR->FInit(&fi))
  55.         wRet=pFR->MessageLoop();
  56.  
  57.     delete pFR;
  58.     return wRet;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CPatronFrame::CPatronFrame
  66.  * CPatronFrame::~CPatronFrame
  67.  *
  68.  * Constructor Parameters:
  69.  *  hInst           HINSTANCE from WinMain
  70.  *  hInstPrev       HINSTANCE from WinMain
  71.  *  pszCmdLine      LPSTR from WinMain
  72.  *  nCmdShow        int from WInMain
  73.  */
  74.  
  75. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  76.     , LPSTR pszCmdLine, int nCmdShow)
  77.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  78.     {
  79.     //CHAPTER5MOD
  80.     m_fInitialized=FALSE;
  81.     //End CHAPTER5MOD
  82.     return;
  83.     }
  84.  
  85.  
  86. CPatronFrame::~CPatronFrame(void)
  87.     {
  88.     //CHAPTER5MOD
  89.     if (m_fInitialized)
  90.         OleUninitialize();
  91.     //End CHAPTER5MOD
  92.     return;
  93.     }
  94.  
  95.  
  96.  
  97.  
  98. //CHAPTER5MOD
  99. /*
  100.  * CPatronFrame::FInit
  101.  *
  102.  * Purpose:
  103.  *  Call OleInitialize then calling down into the base class
  104.  *  initialization.
  105.  *
  106.  * Parameters:
  107.  *  pFI             LPFRAMEINIT containing initialization parameters.
  108.  *
  109.  * Return Value:
  110.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  111.  */
  112.  
  113. BOOL CPatronFrame::FInit(LPFRAMEINIT pFI)
  114.     {
  115.     DWORD       dwVer;
  116.  
  117.     /*
  118.      * We use the OLE versions of these functions now
  119.      * since we'll need them eventually.
  120.      */
  121.     dwVer=OleBuildVersion();
  122.  
  123.     if (rmm!=HIWORD(dwVer))
  124.         return FALSE;
  125.  
  126.     if (FAILED(OleInitialize(NULL)))
  127.         return FALSE;
  128.  
  129.     m_fInitialized=TRUE;
  130.  
  131.     return CFrame::FInit(pFI);
  132.     }
  133. //End CHAPTER5MOD
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. /*
  141.  * CPatronFrame::CreateCClient
  142.  *
  143.  * Purpose:
  144.  *  Constructs a new client specific to the application.
  145.  *
  146.  * Parameters:
  147.  *  None
  148.  *
  149.  * Return Value:
  150.  *  LPCClient       Pointer to the new client object.
  151.  */
  152.  
  153. LPCClient CPatronFrame::CreateCClient(void)
  154.     {
  155.     return (LPCClient)(new CPatronClient(m_hInst));
  156.     }
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. /*
  164.  * CPatronFrame::FRegisterAllClasses
  165.  *
  166.  * Purpose:
  167.  *  Registers all classes used in this application.
  168.  *
  169.  * Parameters:
  170.  *  None
  171.  *
  172.  * Return Value:
  173.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  174.  */
  175.  
  176. BOOL CPatronFrame::FRegisterAllClasses(void)
  177.     {
  178.     WNDCLASS        wc;
  179.  
  180.     //First let the standard frame do its thing
  181.     if (!CFrame::FRegisterAllClasses())
  182.         return FALSE;
  183.  
  184.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  185.     wc.hInstance     = m_hInst;
  186.     wc.cbClsExtra    = 0;
  187.     wc.lpfnWndProc   = PagesWndProc;
  188.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  189.     wc.hIcon         = NULL;
  190.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  191.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  192.     wc.lpszMenuName  = NULL;
  193.     wc.lpszClassName = SZCLASSPAGES;
  194.  
  195.     if (!RegisterClass(&wc))
  196.         return FALSE;
  197.  
  198.     return TRUE;
  199.     }
  200.  
  201.  
  202.  
  203.  
  204.  
  205. /*
  206.  * CPatronFrame::OnCommand
  207.  *
  208.  * Purpose:
  209.  *  WM_COMMAND handler for the Patron frame window that processes extra
  210.  *  File menu items as well as the Page menu.
  211.  *
  212.  * Parameters:
  213.  *  hWnd            HWND of the frame window.
  214.  *  wParam          WPARAM of the message.
  215.  *  lParam          LPARAM of the message.
  216.  *
  217.  * Return Value:
  218.  *  LRESULT         Return value for the message.
  219.  */
  220.  
  221. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  222.     {
  223.     LPCPatronDoc    pDoc;
  224.  
  225.     COMMANDPARAMS(wID, wCode, hWndMsg);
  226.  
  227.     /*
  228.      * Don't bother with anything during first initialization,
  229.      * skipping many GizmoBar notifications.
  230.      */
  231.     if (m_fInit)
  232.         return 0L;
  233.  
  234.     pDoc=(LPCPatronDoc)m_pCL->ActiveDocument();
  235.  
  236.     switch (wID)
  237.         {
  238.         case IDM_FILEPRINT:
  239.             pDoc->Print(m_hWnd);
  240.             return 0L;
  241.  
  242.         case IDM_FILEPRINTERSETUP:
  243.             pDoc->PrinterSetup(m_hWnd, FALSE);
  244.             return 0L;
  245.  
  246.  
  247.         case IDM_PAGENEWPAGE:
  248.             pDoc->NewPage();
  249.             break;
  250.  
  251.         case IDM_PAGEDELETEPAGE:
  252.             pDoc->DeletePage();
  253.             break;
  254.  
  255.         case IDM_PAGENEXTPAGE:
  256.             pDoc->NextPage();
  257.             break;
  258.  
  259.         case IDM_PAGEPREVIOUSPAGE:
  260.             pDoc->PreviousPage();
  261.             break;
  262.  
  263.  
  264.         case IDM_PAGEFIRSTPAGE:
  265.             pDoc->FirstPage();
  266.             break;
  267.  
  268.         case IDM_PAGELASTPAGE:
  269.             pDoc->LastPage();
  270.             break;
  271.  
  272.  
  273.         default:
  274.            return CFrame::OnCommand(hWnd, wParam, lParam);
  275.         }
  276.  
  277.     return 0L;
  278.     }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287. /*
  288.  * CPatronFrame::CreateGizmos
  289.  *
  290.  * Purpose:
  291.  *  Procedure to create all the necessary gizmobar buttons.
  292.  *
  293.  * Parameters:
  294.  *  None
  295.  *
  296.  * Return Value:
  297.  *  UINT            Number of gizmos added to the bar.
  298.  */
  299.  
  300. UINT CPatronFrame::CreateGizmos(void)
  301.     {
  302.     UINT            iLast;
  303.     UINT            uState=GIZMO_NORMAL;
  304.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  305.  
  306.     //Insert the standard ones.
  307.     iLast=CFrame::CreateGizmos();
  308.  
  309.     //Insert Print File Import in the 5th position and account for it in iLast.
  310.     m_pGB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB, NULL, NULL, 6, uState);
  311.     iLast++;
  312.  
  313.     //Add New Page, and Delete Page
  314.     m_pGB->Add(utCmd, iLast++, IDM_PAGENEWPAGE,    m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  315.     m_pGB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB, NULL, m_hBmp, 3, uState);
  316.  
  317.     //Separator
  318.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  319.  
  320.     //First, Prev, Next, Last pages.
  321.     m_pGB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE,    m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  322.     m_pGB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  323.     m_pGB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE,     m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  324.     m_pGB->Add(utCmd, iLast++, IDM_PAGELASTPAGE,     m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  325.  
  326.     return iLast;
  327.     }
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. /*
  336.  * CPatronFrame::UpdateMenus
  337.  *
  338.  * Purpose:
  339.  *  Handles the WM_INITMENU message for the frame window.  Depending
  340.  *  on the existence of an active window, menu items are selectively
  341.  *  enabled and disabled.
  342.  *
  343.  * Parameters:
  344.  *  hMenu           HMENU of the menu to intialize
  345.  *  iMenu           UINT position of the menu.
  346.  *
  347.  * Return Value:
  348.  *  None
  349.  */
  350.  
  351. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  352.     {
  353.     LPCPatronDoc    pDoc;
  354.     BOOL            fOK=FALSE;
  355.     BOOL            fCallDefault=TRUE;
  356.     UINT            uTemp;
  357.     UINT            uTempE;
  358.     UINT            uTempD;
  359.  
  360.     pDoc=(LPCPatronDoc)m_pCL->ActiveDocument();
  361.  
  362.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  363.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  364.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  365.  
  366.     //File menu:  If there is no current document window, disable Import.
  367.     if (m_phMenu[0]==hMenu)
  368.         {
  369.         EnableMenuItem(hMenu, IDM